<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="宿舍名" prop="dormName">
        <el-input
          v-model="queryParams.dormName"
          placeholder="请输入宿舍名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="书院" prop="dormbuildingId">
            <el-select
               v-model="queryParams.dormbuildingId"
               placeholder="请选择书院"
               clearable
               style="width: 200px"
            >
               <el-option
                  v-for="item in dormbuildingList"
                  :key="item.dormbuildingId"
                  :label="item.dormbuildingName"
                  :value="item.dormbuildingId"
               />
            </el-select>
         </el-form-item>
         <el-form-item label="所在楼层" prop="floor">
            <el-select
               v-model="queryParams.floor"
               placeholder="楼层"
               clearable
               style="width: 100px"
            >
            <el-option
                  v-for="dict in cis_dorm_floor"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
         </el-form-item>
         <el-form-item label="床位数" prop="bedNum">
            <el-select
               v-model="queryParams.bedNum"
               placeholder="床位数"
               clearable
               style="width: 100px"
            >
            <el-option
                  v-for="dict in cis_dorm_bed_num"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
         </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:dorm:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dorm:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dorm:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:dorm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dormList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="宿舍id" align="center" prop="dormId" />
      <el-table-column label="宿舍名" align="center" prop="dormName" />
      <el-table-column label="书院" align="center" prop="dormbuildingId"  :formatter="formatterDormbuilding" />
      <el-table-column label="所在层" align="center" prop="floor" />
      <el-table-column label="床位数" align="center" prop="bedNum" />
      <el-table-column label="宿长" align="center" prop="supervisorName" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:dorm:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:dorm:remove']">删除</el-button>
          <el-button link type="primary" icon="User" @click="handleSupervisor(scope.row)" >舍长</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
      <!-- 分配舍长对话框 -->
      <el-dialog :title="title" v-model="openStudent" width="500px" append-to-body>
         <el-form :model="queryStudent" label-width="80px">
            <el-form-item label="角色名称">
               <el-input v-model="form.roleName" :disabled="true" />
            </el-form-item>
            <el-form-item label="权限字符">
               <el-input v-model="form.roleKey" :disabled="true" />
            </el-form-item>
            <el-form-item label="权限范围">
               <el-select v-model="form.dataScope" @change="dataScopeSelectChange">
                  <el-option
                     v-for="item in dataScopeOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value"
                  ></el-option>
               </el-select>
            </el-form-item>
            <el-form-item label="数据权限" v-show="form.dataScope == 2">
               <el-checkbox v-model="deptExpand" @change="handleCheckedTreeExpand($event, 'dept')">展开/折叠</el-checkbox>
               <el-checkbox v-model="deptNodeAll" @change="handleCheckedTreeNodeAll($event, 'dept')">全选/全不选</el-checkbox>
               <el-checkbox v-model="form.deptCheckStrictly" @change="handleCheckedTreeConnect($event, 'dept')">父子联动</el-checkbox>
               <el-tree
                  class="tree-border"
                  :data="deptOptions"
                  show-checkbox
                  default-expand-all
                  ref="deptRef"
                  node-key="id"
                  :check-strictly="!form.deptCheckStrictly"
                  empty-text="加载中，请稍候"
                  :props="{ label: 'label', children: 'children' }"
               ></el-tree>
            </el-form-item>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitDataScope">确 定</el-button>
               <el-button @click="cancelDataScope">取 消</el-button>
            </div>
         </template>
      </el-dialog>
    <!-- 添加或修改宿舍维护对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="dormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="宿舍名" prop="dormName">
          <el-input v-model="form.dormName" placeholder="请输入宿舍名" />
        </el-form-item>
        <el-form-item label="书院" prop="dormbuildingId">
            <el-select
               v-model="form.dormbuildingId"
               placeholder="请选择书院"
               clearable
               style="width: 200px"
            >
               <el-option
                  v-for="item in dormbuildingList"
                  :key="item.dormbuildingId"
                  :label="item.dormbuildingName"
                  :value="item.dormbuildingId"
               />
            </el-select>
         </el-form-item>
         <el-form-item label="所在楼层" prop="floor">
            <el-select
               v-model="form.floor"
               placeholder="请选择所在楼层"
               clearable
               style="width: 200px"
            >
            <el-option
                  v-for="dict in cis_dorm_floor"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
         </el-form-item>
         <el-form-item label="床位数" prop="bedNum">
            <el-select
               v-model="form.bedNum"
               placeholder="请选择床位数量"
               clearable
               style="width: 200px"
            >
            <el-option
                  v-for="dict in cis_dorm_bed_num"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
         </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dorm">
import { listDorm, getDorm, delDorm, addDorm, updateDorm } from "@/api/infomanagement/dorm";
import { listDormbuilding} from "@/api/infomanagement/dormbuilding";

const { proxy } = getCurrentInstance();
//楼层
const { cis_dorm_floor } = proxy.useDict("cis_dorm_floor");
// 床位数
const { cis_dorm_bed_num } = proxy.useDict("cis_dorm_bed_num");

const dormList = ref([]);
const open = ref(false);
const openStudent = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
/* 获取需要选择的数据 */
const dormbuildingList = ref([]);

const data = reactive({
  queryStudent:{
    stuCode: null,
    realname: null,
    idCard: null,
    phone: null
  },
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dormName: null,
    dormbuildingId: null,
    floor: null,
    bedNum: null,
    supervisorId: null,
    supervisorName: null,
    orderNum: null,
    status: null,
  },
  rules: {
  }
});

const { queryParams, form, rules, queryStudent } = toRefs(data);
/** 查询书院列表 */
function getDormbuildingList() {
  listDormbuilding().then(response => {
    dormbuildingList.value = response.rows;
  });
}
/** 格式化列表书院id */
const formatterDormbuilding = (row) => {
  const a = dormbuildingList.value.find((item)=>item.dormbuildingId===row.dormbuildingId)
  return a?.dormbuildingName
}

/** 查询宿舍维护列表 */
function getList() {
  loading.value = true;
  listDorm(queryParams.value).then(response => {
    dormList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    dormId: null,
    dormName: null,
    dormbuildingId: null,
    floor: null,
    bedNum: null,
    supervisorId: null,
    supervisorName: null,
    orderNum: null,
    status: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("dormRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.dormId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加宿舍维护";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _dormId = row.dormId || ids.value
  getDorm(_dormId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改宿舍维护";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dormRef"].validate(valid => {
    if (valid) {
      proxy.$modal.loading("正在提交中...");
      if (form.value.dormId != null) {
        updateDorm(form.value).then(response => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      } else {
        addDorm(form.value).then(response => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _dormIds = row.dormId || ids.value;
  proxy.$modal.confirm('是否确认删除宿舍维护编号为"' + _dormIds + '"的数据项？').then(function() {
    return delDorm(_dormIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('infomanagement/dorm/export', {
    ...queryParams.value
  }, `dorm_${new Date().getTime()}.xlsx`)
}
function handleSupervisor(row){

}

getDormbuildingList()
getList();
</script>
